/**
 * Created by limeng on 16/6/6.
 */
delegateApp.controller("RegistProposalCtrl", function (
    $scope, $rootScope, $http, $routeParams) {
    $scope.proposalId = $routeParams.proposalId;
    $scope.proposal = {};
    $scope.secondMotionList = [];
    $scope.departments = [];
    $scope.secondMotionPageNo = 1;
    $scope.showMoreMotion = false;

    $http.get('/proposal/'
        + $scope.proposalId
    ).success(
        function (data) {
            if (data.state == true) {
                $scope.proposal = data.proposal;
                var contentElem = document.getElementById("proposalContent");
                contentElem.innerHTML = $scope.proposal.content;
                contentElem.style.padding = 15;

                $scope.registProposal = {};
                if ($scope.proposal.hostDepartment) {
                    $scope.registProposal.hostDepartmentId = $scope.proposal.hostDepartment.id;
                }
                $scope.registProposal.type = "";

                $scope.$watch('registProposal.hostDepartmentId', $scope.resetAssistDepartments);
                $scope.resetAssistDepartments();
            } else {
                showAlert('查询失败', '原因是:'+data.result);
            }
        }
    ).error(
        function () {
            showAlert('查询失败', '请联系开发者进行维护');
        }
    );

    $scope.refreshMotionList = function() {
        $http.get('/second_motion/' + $scope.proposalId
            + '/10/' + $scope.secondMotionPageNo
        ).success(
            function (data) {
                if (data.state == true) {
                    for (var i = 0; i < data.list.length; i++) {
                        $scope.secondMotionList.add(data.list[i]);
                    }
                    if ($scope.secondMotionPageNo < data.pageCount) {
                        $scope.showMoreMotion = false;
                    } else {
                        $scope.showMoreMotion = true;
                    }
                } else {
                    showAlert('查询失败', '原因:'+data.result);
                }
            }
        ).error(
            function () {
                showAlert('查询失败', '附议信息数据库出现异常');
            }
        );
    };
    $scope.refreshMotionList();

    $http.get("/department/").success(function (data) {
        if (data) {
            $scope.departments = data;
        } else {
            bootbox.alert("获取部门信息失败!");
        }
    });

    $scope.openDialog = function () {
        $("#registProposalDialog").modal();
    };

    $scope.doRegist = function () {
        var postData = {};
        postData.type = $scope.registProposal.type;
        postData.hostDepartmentId = $scope.registProposal.hostDepartmentId;
        postData.id = $scope.proposal.id;
        var cb_assistDepartment = document.getElementsByName("cb_assistDepartment");
        if (cb_assistDepartment.length > 0) {
            postData.assistDepartmentIdList = [];
            for (var i = 0; i < cb_assistDepartment.length; i++) {
                if (cb_assistDepartment[i].checked) {
                    postData.assistDepartmentIdList.push(cb_assistDepartment[i].value);
                }
            }
        }

        $http.post("/proposal/registPass", postData).success(function (data) {
            if (data.status == true) {
                bootbox.alert("立案成功!", function () {
                    history.go(-1);
                });
            } else {
                bootbox.alert("创建失败, 原因是" + data.result);
            }
        }).error(function () {
            bootbox.alert("服务器没有响应请求!");
        });
    };

    $scope.proposalReject = function () {
        bootbox.prompt('请输入拒绝立案的理由：', function (result) {
            if (result != null) {
                $http.post('/proposal/registFailed/' + $scope.proposalId, result)
                    .success(function (data) {
                        if (data.state == true) {
                            bootbox.alert("已转为建议！", function () {
                                history.go(-1);
                            });
                        } else {
                            bootbox.alert("操作失败，原因是" + data.result);
                        }
                    }).error(function () {
                        bootbox.alert("服务器遇到故障！")
                    });
            }
        });
    };

    $scope.resetAssistDepartments = function () {
        $scope.assistDepartments = [];
        var departments = $scope.departments;
        for (var i = 0; i < departments.length; i++) {
            if (departments[i].id != $scope.registProposal.hostDepartmentId) {
                $scope.assistDepartments.push(departments[i]);
            }
        }
    };
});